; Zinc-OS-boot
; Tab=4

    org 0x7c00

CYLS equ 20     ;cylinders

BaseOfStack equ 0x7c00

; ###### FAT12 fs ######

    jmp short Label_Start
    nop
; ### BS  == Boot Sector
; ### BPB == BIOS Parameter Block
    BS_OEMName  db  "ZINCboot"
    BPB_BytesPerSec dw  512
    BPB_SecPerClus  db  1
    BPB_RsvdSecCnt  dw  1
    BPB_NumFATs db  2
    BPB_RootEntCnt  dw  224
    BPB_TotSec16    dw  2880
    BPB_Media   db  0xf0
    BPB_FATSz16 dw  9
    BPB_SecPerTrk   dw  18
    BPB_NumHeads    dw  2
    BPB_HiddSec dd  0
    BPB_TotSec32    dd 2880
    BS_DrvNum   db  0
    BS_Reserved1    db  0
    BS_BootSig  db  0x29
    BS_VolOD    dd  0
    BS_VolLab   db  "boot loader"
    BS_fileSystemType   db  "FAT12   "
    
    resb 18

; ###### main boot code ######

Label_Start:

;======= init reg

    mov ax, cs
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov sp, BaseOfStack

    mov ax, 0x0820
    mov es, ax
    mov ch, 0
    mov dh, 0
    mov cl, 2
readloop:
    mov si, 0
retry:
    mov ah, 0x02    ;read mode
    mov al, 1
    mov bx, 0
    mov dl, 0x00
    int 13h
    jnc next
    add si, 1
    cmp si, 5
    jae error
    mov ah, 0x00
    mov dl, 0x00
    int 13h
    jmp retry

next:
    mov ax, es
    add ax, 0x0020
    mov es, ax
    add cl, 1
    cmp cl,18
    jbe readloop
    mov cl, 1
    add dh, 1
    cmp dh, 2
    jb readloop
    mov dh, 0
    add ch, 1
    cmp ch, CYLS    
    jb readloop

;======= jump to kernel

    mov [0x0ff0], ch
    jmp 0xc200

error:
    mov si, msg
putloop:
    mov al, [si]
    add si, 1
    cmp al, 0
    je fin
    mov ah, 0x0e
    mov bx, 15
    int 10h
    jmp putloop

fin:
    hlt
    jmp fin

msg:
    db 0x0a, 0x0a
    db "load error"
    db 0x0a
    db 0

;======= fill zero until whole boot sector

    resb 0x7dfe-$

;======= boot sector flag

    db 0x55,0xaa
